﻿using System.Collections.Generic;
using System.IO;

namespace Toybox.SCM.Models.Accounts
{
	using NicoSharp.Abstracts;
	using NicoSharp.Utilities;

	public class AccountGroup : AbstractNotifyObject
	{

		#region Const

		protected const string DIRECTORY_PATH_COMMENTS = @"comments";
		protected internal const string CSV_START_TAG = @"[account]";

		#endregion Const


		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		public AccountGroup(AccountCollection accounts)
		{
			// デフォルト値
			this.Is184 = false;
			this.DirectoryRoot = Path.Combine(StaticData.DirectoryPath, DIRECTORY_PATH_COMMENTS);

			this.Accounts.AddAccounts(accounts);
		}

		public AccountGroup(AccountCollection accounts, string[][] elements)
			: this(accounts)
		{
			bool bResult = false;
			uint uiResult = 0;

			for (int n = 0; n < elements.Length; n++)
			{
				switch (n)
				{
					case 0:	// 固定
						this.Name = elements[n][0];
						if (bool.TryParse(elements[n][1], out  bResult))
							this.Is184 = bResult;
						this.DirectoryRoot = elements[n][2];
						break;
					case 1:	// 機能
						break;
					case 2:	// アカウント
						string[] uIdList = elements[n++];
						string[] isUseList = elements[n++];
						string[] priorityList = elements[n++];

						for (int m = 0; m < uIdList.Length && m < isUseList.Length && m < priorityList.Length; m++)
						{
							var pa = this.Accounts[uIdList[m]];
							if (bool.TryParse(isUseList[m], out  bResult))
								pa.IsUse = bResult;
							if (uint.TryParse(priorityList[m], out  uiResult))
								pa.Priority = uiResult;
						}
						return;
				}
			}
		}

		#endregion Constructor


		#region Public Members

		/// <summary>
		/// Name
		/// </summary>
		public string Name
		{
			get { return this._name; }
			set
			{
				if (this._name == value) return;
				this._name = value;
				base.OnPropertyChanged("Name");
			}
		}
		private string _name;

		/// <summary>
		/// Directory Root
		/// </summary>
		public string DirectoryRoot
		{
			get { return this._directoryRoot; }
			set
			{
				if (this._directoryRoot == value) return;
				this._directoryRoot = value;
				base.OnPropertyChanged("DirectoryRoot");
			}
		}
		private string _directoryRoot;

		/// <summary>
		/// Anonymous Comment Flag
		/// </summary>
		public bool Is184
		{
			get { return this._is184; }
			set
			{
				if (this._is184 == value) return;
				this._is184 = value;
				base.OnPropertyChanged("Is184");
			}
		}
		private bool _is184;

		// TODO: ボタン機能ONOFF

		/// <summary>
		/// Accounts
		/// </summary>
		public PriorityAccountCollection Accounts
		{
			get { return this._accounts ?? (this._accounts = new PriorityAccountCollection()); }
			set
			{
				if (this._accounts == value) return;
				this._accounts = value;
				base.OnPropertyChanged("Accounts");
			}
		}
		private PriorityAccountCollection _accounts;

		#endregion Public Members


		#region Public Methods

		#endregion Public Methods

		#region OverrideMethods

		/// <summary>
		/// 文字列化
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			string result = CSV_START_TAG + "\n"; ;
			var list = new List<string>();

			// 固定
			list.Clear();
			list.Add(this.Name.ToString());
			list.Add(this.Is184.ToString());
			list.Add(this.DirectoryRoot);
			result += CsvUtility.Compose(list.ToArray()) + "\n";

			// TODO:ボタン機能
			list.Clear();

			result += CsvUtility.Compose(list.ToArray()) + "\n";

			// アカウント
			result += this.Accounts.ToString();

			return result + "\n";
		}

		#endregion OverrideMethods

	}
}
